import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "@/store/index"
//
import {createRouterConfig, createDynamicRoutes} from "@/config/router"

Vue.use(VueRouter)
// 路由实例创建方法
const createRouter = (routes) => new VueRouter({...createRouterConfig, routes})
// 创建路由实例
const router = createRouter(createDynamicRoutes(store.getters.sourceMenus))

// 创建 重构路由方法并挂载到路由实例的原型链上

router.__proto__.$rebuild = function () {
    const routes = createDynamicRoutes(store.getters.sourceMenus)
    const nr = createRouter(routes);
    this.matcher = nr.matcher;
}

// 路由守护
router.beforeEach((to, from, next) => {
    if (to.path === from.path) {
        return;
    }
    next();
})


export default router